const Base = require('./base.js');
const wxAuthModel = think.model('wxauth','mysql2');
const shareModel = think.model('share','mysql2');
const userModel = think.model('user','mysql2');
const wechat = think.service('wechat');
module.exports = class extends Base {

    async __before(){
        // const userSession = await this.session('userInfo');
        // if(!think.isEmpty(userSession))
        // {
        //     return userSession;
        // }
    }

    /* @display 保存微信用户资料信息到wxauth db
     * @param code $_GET['openid']
     * @return void
     **/
      async callBackAction() {
        await this.session('userinfo',null);
        let openid=this.get('openid');
        if(openid)
        {
          let data = await wxAuthModel.field('id,openid').where({'openid': openid}).find();
          // console.log(data.openid);
          if (!data.openid)
          {
            const wxUserInfo = await wechat.getWxUserInfo(openid);
            console.log(wxUserInfo.openid);
            let field =
            {
              openid: wxUserInfo.openid,
              nickname: wxUserInfo.nickname,
              sex: wxUserInfo.sex,
              country: wxUserInfo.country,
              province: wxUserInfo.province,
              city: wxUserInfo.city,
              headimgurl: wxUserInfo.headimgurl,
              ip: this.ip,
              ctime: Date.now(),
            };
            let insertId = await wxAuthModel.add(field);
            if (insertId)
            {
              data = {openid: 'openid', id: 'insertId'};
              this.session('userinfo', data);
            }

          };
          this.session('userinfo', data);
          return this.redirect('/user');//登录成功将用户信息写入session，返回到user首页。

        }
        return this.body='授权失败稍后重试';
        // return this.display();
      }

    /* @display 微信分享记录
     * @param
     *      NULL
     * @return array
     **/

    async shareAction(){
        const userSession= await this.session('userinfo');
        let data={
            flag:0,
            msg:'缺少参数',
        };
        console.log(userSession);
        if(userSession.id)
        {
            const field={
                wxid:userSession.id,
                ip: this.ip,
                ctime: Math.floor(Date.now()/1000),
            };
            let insertId = await shareModel.add(field);
            if(insertId){
                data={
                    flag:insertId,
                    msg:'分享成功',
                };
            }
        }
        return this.json(data);

    }

    /* @display 前50名排行榜
     * @param
     *      NULL
     * @return array
     **/

    async rankListAction(){
        const userSession= await this.session('userinfo');
        let data={
            flag:0,
            msg:'缺少参数',
        };
        console.log(userSession);
        if(userSession.id)
        {
            let insertId = await userModel.select();
            if(insertId){
                data={
                    flag:insertId,
                    msg:'分享成功',
                };
            }
        }
        return this.json(data);

    }

};
